var util = require('../../utils/util');
var wxApi = require('../../utils/wxApi');
var wxRequest = require('../../utils/wxRequest');
import config from '../../utils/config';
import { $wuxDialog } from '../../components/wux'

var app = getApp();
var that;


Page({
    data: {
        user: {}
      ,isload:0
      ,sysset:{}
      ,istcVisible: false
      ,istcVisible2: false
      ,opop:0
      ,stop: 1000
      ,sleft: 1000

      ,stepall:0
      ,step:0
      ,date:null
      ,isgetkb:false
      ,isgl:false
      ,isadmin:false
      ,isload:true
      ,kbobj:{kb:0,kbyd:0,kbxf:0}
      ,kblimit:0

      ,xh_cake:0
      ,xh_cal:0
      ,encryptedData:null
      ,pmday:0
      ,wh:600
      ,sh:296
      ,swidth:375
        ,code:""
        ,isgly:false
  },
  onPullDownRefresh:function(){
    that.loadData();
  },
  onShow:function(){
    that.loadData();
  },
  onReady:function(){
      that.data.isload=0;

      that.data._sdesc="个人主页";
      wx.setNavigationBarTitle( { title: that.data._sdesc });
    
  },
  closeTc:function(){
    this.setData({
      istcVisible: false,
    });
  },
  closeTc2:function(){
    this.setData({
      istcVisible2: false,
    });
  },
  onPullDownRefresh:function(){
    that.loadData();
  },
  onLoad: function (options) {
      that = this;
      wx.getSystemInfo({
          success: function(res) {
              var w = parseFloat(res.windowWidth);
                w = parseFloat( w * 0.95);
              var h = parseFloat( (w * 78 )/100);

              that.setData({
                  swidth:w
                  ,sh :h
              })
          }
      })
    //that.loadData();
  },
  onShareAppMessage: function () {
      return app.getShare();
  },
  loadData:function(){

    util.showLoading();

    app.getUserInfo()
    .then(res => {
        console.log(res);
      var bl = false;
      var bl2 =false;
      var bl3 =false;
      if(res.isgl==1){
        bl =true;
      }
      if(res.isadmin ==1){
        var bl2 = true;
      }
      if(res.isgly ==1){
          var bl3 = true;
      }

      that.setData({
        user:res
        ,isgl:bl
        ,isadmin:bl2
        ,isgly:bl3
      })
       
      return wxApi.wxGetWeRunData()();
    })
    .then(res => {
        console.log(res);


      var url = config.url_user_save_run;
      var params = {
       ed: res.encryptedData,
       key: util.getkey(),
       iv:util.getiv()
      }
      return wxRequest.postRequest(url, params);
    })
    .then(res=>{
      console.log(res);
      // var s = util.formatTime(new Date(parseInt(res.timestamp) * 1000));
      var step = parseInt(res.step);
      var kb = parseInt(res.kb);
      if(kb>0){
        kb = true;
      }else{
        kb=false;
      }

      that.setData({
        step:step,
        date:res.udate,
        isgetkb:kb
      });

        //获取用户的个人信息 包括读取接口 url_day_pm
      var url = config.url_user_get;
      var params = {
       id:app.globalData.user.uid
      }
      return wxRequest.postRequest(url, params);

    })
    .then(res=>{
        console.log(res);
      // res.user 用户的信息  res.dkuser  dk用户的信息
      // 
      res.dkuser = util.toobj(res.dkuser);
      that.setData({
          'kbobj.kb':res.dkuser.kbnow
      });
      if(res.dkuser && res.dkuser.success ){
         that.setData({
          'kbobj.kbyd':res.user.ydkb,
          'kbobj.kbxf':res.dkuser.kball
        });
      }
      var url = config.url_get_sysset;
      var params = {}
      return wxRequest.postRequest(url, params);
    })
    .then(res=>{
        console.log(res);
      // res.user 用户的信息  res.dkuser  dk用户的信息
      that.setData({
          sysset:res,
          stepall:res.kblimit
      });

        //获取用户的个人信息 包括读取接口 
      var url = config.url_day_pm;
      var params = {
          id:app.globalData.user.uid
      }
      return wxRequest.postRequest(url, params);

    })
    .then(res=>{
        console.log(res);
        if(util.judge.isObject(res)){
            that.setData({
                pmday:res.dpm
            });
        }
        var tsi = wx.getSystemInfoSync();
        that.setData({
            wh:tsi.windowHeight-30
        });
    })
    .catch(res => {
        console.log(res);
      util.alert_catch(res);
    })
    .finally(function (res) {
      util.hideLoading();
      wx.stopPullDownRefresh();
    });
 
  }
 
  ,btnGetkb:function(){
      wx.navigateTo({
          url: 'ktlist_js'
          ,fail:function(e){
              console.log(e);
          }
      })
  }
  ,btnKtlist:function(){
    wx.navigateTo({
      url: '../user/ktlist?type=2'
    })
  },btnSqm:function(){
    wx.navigateTo({
      url: '../user/sqm'
    })
  },
  btnHb:function(){
      if(that.data.isgl){
          wx.navigateTo({
              url: '../user/dayhis'
          });
      }else{
          wx.navigateTo({
              url: '../user/regnew?back=1'
          })
      }

  },
  btnSet:function(){
      wx.navigateTo({
          url: '../index/set'
      })
  },
  btnhy:function(){
      wx.navigateTo({
          url: '../index/hyrs'
      })
  }
    ,btnalljs:function(){
        wx.navigateTo({
            url: '../user/ktlist_js?isall=1'
        })

    }
,btnlj:function(){
    wx.navigateTo({
        url: '../user/ljlist'
    })
}


    

})